<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model\D;
use app\index\model\Phone;
use app\index\model\Fz;
use app\index\model\Books;
	class Api extends Controller
	{
		public function index(){
			// echo "输出";
			// $s=array_map(function($x){
			// 	return $x*2;
			// },[1,2,3]);
			// echo \var_dump($s);
			header("Access-Control-Allow-Origin:http://127.0.0.1:8848");
			$data['b']=input("post.user","haha");
			$data['c']=input("get.user","aaa");
			
			\header("set-Cookie:"."sessionid=38afes7a8; HttpOnly; Path=/");
			$data['a']="123";
			return json($data);
			
		}
		
		public function show(){
			 $user=input("post.user");
			 $pass=input("post.pass");
			// $user=input("get.user");
		 //    $pass=input("get.pass");
			$data["user"]=$user;
			$data["pass"]=$pass;
			header("Access-Control-Allow-Origin:http://localhost:8080");
			return json($data);
		}
		public function up(){
			header("token","qw");
			 header('Access-Control-Allow-Headers:token');
			header("Access-Control-Allow-Origin:http://localhost:8080");
			if(!empty($_FILES)){
				$data["info"]=$_FILES["file"];
				$a=Request::instance()->header("token");
				$data["head"]=$a;
				return json($data);
			}
			else{
				$data["head"]=\header("token");
					$data["info"]="无文件上传";
					return \json($data);
			}
		}
		//分页
		public function page(){
			header("Access-Control-Allow-Origin:http://localhost:8080");
			//header('Access-Control-Allow-Headers:token');
			header("Access-Control-Allow-Credentials:true");
			header("Access-Control-Allow-Method:get,post");
			//$a=\input("post.a");
			$a=input("get.a");
			//$r=Request::instance()->header("token");
			//$d["c"]=$r;post提交
			$p=new D();
			
			$d["b"]=$p->page($a);
			return json($d);
		}
		//手机注册
		public function zhu(){
			header("Access-Control-Allow-Origin:http://localhost:8080");
			//header('Access-Control-Allow-Headers:token');
			header("Access-Control-Allow-Credentials:true");
			header("Access-Control-Allow-Method:get,post");
			$user=input("post.phone");
			$p=new Phone();
			$d=$p->zhu_ce($user);
			return json($d);
		}
		//首页搜索
		public function search(){
		//	header("Access-Control-Allow-Origin:*");
			
			//header('Access-Control-Allow-Headers:token');
			header("Access-Control-Allow-Credentials:true");
			header("Access-Control-Allow-Method:get,post");
			$name=input("get.inf");
			if(empty($name)){
				//等同于var data={value:""}难怪会有关联数组	
				$fz=new Fz();
				$info=$fz->search();
				$da=null;
				foreach($info as $key=>$value){
						$da=$da.$value["fztupian"].',';
					
				}
				$r=explode(",",$da);	
				$f=array_pop($r);
				$arr=[];
				for($i=0;$i<=count($r)-1;$i++){
				$data["value"]=$r[$i];
				array_push($arr,$data);}
				return json($arr);
			}
			}
		//书搜索
		public function book(){
			header("Access-Control-Allow-Origin:http://localhost:4000");
			//header('Access-Control-Allow-Headers:token');
			header("Access-Control-Allow-Credentials:true");
			header("Access-Control-Allow-Method:get,post");
			$name=input("get.book");
			
			if(empty($name)){
				//等同于var data={value:""}难怪会有关联数组	
				$book=new Books();
				$data=$book->sou();
				
				$nb=null;
				$nc=null;
				foreach($data as $key=>$value){
				$nb=$nb.$value["name"]." ";
				$nc=$nc.$value["zuozhe"]." ";
				}
				
				$b=explode(" ",$nb);
				$c=explode(" ",$nc);
				array_pop($b);
				array_pop($c);
				$arr=[];
				for($i=0;$i<=count($b)-1;$i++){
					//echo $b[$i];
					$da["value"]=$b[$i];
					$da["name"]=$c[$i];
					array_push($arr,$da);
				}
				return json($arr);
			}
			
		}
		public function upload(){
			//header("Access-Control-Allow-Origin:http://127.0.0.1:8848");
			//header("Access-Control-Allow-Methods:POST");			
			//$files=\input("get.a",array("a"=>"1"));
			if(isset($_FILES)){
				if(empty($_FILES['file'])){
					$data['err']="不存在";
					return \json($data);
				}
				else{
					$data['a']=$_FILES['file'];
					$path="./img/".$_FILES['file']['name'];
					if(move_uploaded_file($_FILES['file']['tmp_name'],$path)){
						$info['inf']="success";
						return json($info);
					}	
					else{
						return json($data);
					}
				}			
			}
			else{
				$data['err']="err";
				return json($data);			}		
		}
		public function lei(){
			$book=new Books();
			$data=$book->lei();
			return \json($data);
		}
	}